package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;

import com.atguigu.springcloud.extend.RedisLock;
import com.atguigu.springcloud.feign.appoint.AppointService;
import com.atguigu.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaolong
 * @version 1.0
 * @description: TODO
 * @date 2021/9/12 16:51
 */
@RestController
@Slf4j
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;


    @Autowired
    private AppointService appointService;

//    @Autowired
//    private RabbitmqUtil rabbitmqUtil;

    @Autowired
    private RedisLock redisLock;

    @GetMapping(value = "/consumer1/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }


    /**
     * @param
     * @return
     * @description: 模拟超时
     */
    @GetMapping(value = "/consumer/payment/feign/timeout")
    public String paymentFeignTimeOut() {
        return paymentFeignService.paymentFeignTimeOut();
    }


    /**
     * @param
     * @return
     * @description: 测试feign-sdk模块
     */
    @RequestMapping("/feign-sdk")
    public String test1() throws Exception {
        String data = appointService.paymentFeignTimeOut();

        ////直接抛异常，是为了方便测试服务熔断和降级。
        System.out.println("获取到feign-sdk的值为：" + data);
        return data;
    }


//    @RequestMapping("rabbitmq-sdk")
//    public void test2(){
//        BurnTaskInfoDto burnTaskInfoDto = new BurnTaskInfoDto();
//        burnTaskInfoDto.setTaskId(123456);
//        burnTaskInfoDto.setTaskName("测试");
//        rabbitmqUtil.sendTask(burnTaskInfoDto);
//    }


    @RequestMapping("redis-sdk")
    public void test3() {
        boolean lock = redisLock.tryLock("testLock", "加锁", 200, 50);
        System.out.println(lock);
        if (lock) {

            System.out.println("恭喜获得锁");

//            redisLock.unLock("testLock","解锁");
        }

    }


}
